package com.example.listener.page

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.example.listener.databinding.FragmentBrightnessBinding
import com.seed.launcher.listener.BrightnessListener

/**
 *
 * @author: xct
 * create on: 2022/8/30 10:57
 *
 */
class BrightnessFragment : Fragment() {
    private lateinit var viewBinding: FragmentBrightnessBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        return FragmentBrightnessBinding.inflate(inflater, container, false)
            .also { viewBinding = it }
            .root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val context = view.context
        val listener =
            com.seed.launcher.listener.BrightnessListener(context) {
                showValue(it)
            }
        viewLifecycleOwner.lifecycle.addObserver(object :DefaultLifecycleObserver{
            override fun onCreate(owner: LifecycleOwner) {
                listener.register()
            }

            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                listener.unregister()
                viewLifecycleOwner.lifecycle.removeObserver(this)
            }
        })
//        showValue(SystemUtil.getSystemBrightness(context.contentResolver))
    }

    private fun showValue(it: Float) {
        viewBinding.tvValue.text = "当前亮度值：$it"
    }
}